'use strict';

define(function(require,exports,module){
	var React = require('reactAddons');
	var $ = require('jquery');
	var jPages = require('jPages');

	/*DATE下拉框*/
	var SelectOptionDate = require('components/partial/SelectOptionDate');

	var publicObj = require('lib/public');

    //Pager
    var Pager = require('components/partial/Pager');

    module.exports = React.createClass({displayName: "exports",

		handleDateChange:function(event){
			var date = event.target.value;
			this.loadData(date);
			this.setState({date:date});
		},
		getInitialState:function(){
			return {
				data:[],
				data_sum:0,
                page:1,
                pre_stop:true,
                next_stop:false
			};
		},
        loadDataSum:function(){
            var token = publicObj.get_token();
            var sql = "select count(*) as sum from qeeniao.task_queue";
            var data = new Object();
            data.sql = sql;
            data.access_token = token;

            $.ajax({
                url:this.props.url,
                data:data,
                type:'post',
                dataType:'json',
                success:function(o){
                    if(this.isMounted()){
                        this.setState({'data_sum':o[0].sum});
                    }
                }.bind(this),
                error:function(xhr,status,err){
                    console.error('获取数据失败!',status,err.toString());
                }.bind(this)
            })
        },
        listData:function(p){
            this.setState({'data_list':[]});
            var token = publicObj.get_token();

            p = parseInt(p);
            var page = p - 1;
            page = page > 0 ? page : 0;
            var pagesize = 10;
            var limit = " limit "+(page * pagesize)+","+pagesize+" ";

            var data = new Object();
            data.sql = "select " +
                "autokid," +
                "agentpos," +
                "openrace," +
                "priority," +
                "trytimes," +
                "task_info,"+
                "task_flag,"+
                "FROM_UNIXTIME(UNIX_TIMESTAMP(startime),'%Y-%m-%d  %H:%i:%s') as startime," +
                "FROM_UNIXTIME(UNIX_TIMESTAMP(addtime),'%Y-%m-%d  %H:%i:%s') as addtime," +
                "FROM_UNIXTIME(UNIX_TIMESTAMP(begtime),'%Y-%m-%d  %H:%i:%s') as begtime," +
                "FROM_UNIXTIME(UNIX_TIMESTAMP(endtime),'%Y-%m-%d  %H:%i:%s') as endtime," +
                "task_type," +
                "owner " +
                "from qeeniao.task_queue " +
                "order by addtime DESC "+limit;
            data.access_token = token;

            $.ajax({
                url:this.props.url,
                data:data,
                type:'post',
                dataType:'json',
                success:function(o){
                    if(this.isMounted()) {
                        this.setState({'data': o});

                        if (o.length < pagesize) {
                            this.setState({'next_stop': true});
                        } else {
                            this.setState({'next_stop': false});
                        }

                        if ((p - 1) <= 0) {
                            this.setState({'pre_stop': true});
                        } else {
                            this.setState({'pre_stop': false});
                        }

                        this.setState({page: p});
                    }

                }.bind(this),
                error:function(xhr,status,err){
                    console.error('获取数据失败!',status,err.toString());
                }.bind(this)
            })
        },
        componentDidMount:function(){
            this.listData(1);
            this.loadDataSum();
        },
		componentDidUpdate:function(){

		},

		render:function(){
			var data_list = this.state.data.map(function(i){
				return (
					React.createElement("tr", null, 
						React.createElement("td", null, i.agentpos), 
						React.createElement("td", null, i.openrace), 
						React.createElement("td", null, i.priority), 
                        React.createElement("td", null, i.trytimes), 
                        React.createElement("td", null, i.startime), 
                        React.createElement("td", null, i.addtime), 
                        React.createElement("td", null, i.begtime), 
                        React.createElement("td", null, i.endtime), 
                        React.createElement("td", null, i.task_type), 
                        React.createElement("td", null, i.owner), 
                        React.createElement("td", {style: {'wordBreak':'break-all','width':'100px'}}, i.task_flag)
					)

				)
			});

            var pager_props = {
                page : this.state.page,
                pre_stop:this.state.pre_stop,
                next_stop:this.state.next_stop,
                listData : this.listData
            };

			return (
				React.createElement("div", {className: "panel panel-default", style: {marginTop:10+'px'}}, 
					React.createElement("div", {className: "panel-heading"}, 
						React.createElement("div", {className: "row panel-title"}, 
							React.createElement("div", {className: "col-xs-12"}, 
								React.createElement("h4", {className: ""}, 
									this.props.title, " :", 
									React.createElement("span", {className: "red"}, this.state.data_sum, "个")

								)
							)
						)

					), 
					React.createElement("div", {className: "panel-body"}, 
						React.createElement("table", {className: "table table-striped table-bordered table-hover"}, 
							React.createElement("thead", null, 
							React.createElement("tr", null, 
								React.createElement("th", null, "Agentpos"), 
								React.createElement("th", null, "Openrace"), 
								React.createElement("th", null, "Priority"), 
                                React.createElement("th", null, "Try Times"), 
                                React.createElement("th", null, "Start Time"), 
                                React.createElement("th", null, "Add Time"), 
                                React.createElement("th", null, "Beg Time"), 
                                React.createElement("th", null, "End Time"), 
                                React.createElement("th", null, "Task Type"), 
                                React.createElement("th", null, "Owner"), 
                                React.createElement("th", null, "Task Flag")
							)
							), 
							React.createElement("tbody", {id: "itemContainer"}, 
							data_list
							)
						)
					), 
					React.createElement("div", {className: "panel-body"}, 
                        React.createElement(Pager, React.__spread({},  pager_props))
					)
				)
			);
		}
	});
});